/**
* Class Name: iFulfillQueryReasonCodeCntrl 
* Author: Accenture
* Date: 06-dec-2011
* Requirement/Project Name: iFulfill
* Requirement/Project Description: To display query reason code on queries page layout
**/

public class iFulfillQueryReasonCodeCntrl 
{
    private String productFamily;
    private Queries__c  queryObj;  
    public List<QueryReasonsCodeWrapper> qrCodeWrapperList {get;set;}
    
     /*
     *  QueryReasonsCode Wrapper Class 
     *
     **/
    
    public class QueryReasonsCodeWrapper
    {
        public Boolean rowSelection {get;set;}
        public Query_Reason_Code__c queryReasonCode {get;set;}
                
        public QueryReasonsCodeWrapper(Boolean sel , Query_Reason_Code__c queryResCode )
        {
                rowSelection =sel;
                queryReasonCode = queryResCode; 
        }
    }
    /* 
     *  Constructor
     *
     **/
    public iFulfillQueryReasonCodeCntrl(ApexPages.StandardController controller) 
    {
           queryObj = (Queries__c)controller.getRecord();
    }


    /* 
     *   Fetch the query reason from  query ReasonsCode object
     *
     **/          
    public void listQueryReasonCode()
    {
        qrCodeWrapperList = new List<QueryReasonsCodeWrapper>();
        List<Query_Reason_Code__c>   queryReasonCodeList  = [Select Reason_Code__c from Query_Reason_Code__c where Product_Family__c = :queryObj.Product_Family__c];
        List<String> queryReasonCode  =  null;                
        Set <String> qrcSet = new Set<String>(); 
        if(queryObj.Reason_Code_Txt__c !=null)
        {
           queryReasonCode  =  queryObj.Reason_Code_Txt__c.split(';');
        }
        
        if(queryReasonCode != null)
        {
            for (String obj : queryReasonCode  )
            {
                 qrcSet.add(obj);
            }
        }
        for(Query_Reason_Code__c qrcObj: queryReasonCodeList  )
        { 
            qrCodeWrapperList.add(new QueryReasonsCodeWrapper(qrcSet.contains(qrcObj.Reason_Code__c),qrcObj));
        }          
    }  
    /*
     *   saves the selected query reason code in queries object
     *
     **/
    public pagereference addQueryReasons()
    {
         List <Queries__c> queryList = [Select Reason_Code_Txt__c from  Queries__c  where id =:queryObj.id];
         String temp = '';
         for (QueryReasonsCodeWrapper wrapObj : qrCodeWrapperList )
         {
             if(wrapObj.rowSelection)
             {
                   temp =   wrapObj.queryReasonCode.Reason_Code__c + ';' + temp;
             }
         } 

        try 
        {
            if(queryList.size() > 0)
            {
                Queries__c obj = queryList.get(0);
                obj.Reason_Code_Txt__c = temp.length() > 0 ? temp.subString(0,temp.length() -1 ) : ''; 
                System.debug ('Update date '+ obj);    
                Update obj ; 
                System.debug ('Update fired ');    

            }
        }
        catch (Exception e)
        {
             GeneralConstants.mailAnyExceptionToMe('iFulfillQueryReasonCodeCntrl' ,'addQueryReasons',e);
        }
        return null;        
     }
     
     /** Test method.***/    
      
      public  static testmethod void testQueryReasonCode() 
      {  
      
           Profile p = [select id from profile where name='System Administrator'];         
           User u = new User(alias = 'sysad', email='sysadmin@accenture.com',emailencodingkey='UTF-8', lastname='Tes12', languagelocalekey='en_US',localesidkey='en_US', profileid = p.Id,timezonesidkey='Asia/Calcutta', username='testad123@testorg.com');           
           insert u;
           Account acc = new Account(name= 'xyx');
           Query_Reason_Code__c qrc= new Query_Reason_Code__c(Product_Family__c='Audio', Reason_Code__c='URL');
           insert qrc;
           Fullfilment_Item__c FI4 = new Fullfilment_Item__c(Account__c = acc.id,Main_Listing_Phone_Number__c = '104560',Listed_Name__c ='XIne',Heading_Name__c ='Test Get grouped',
           Item_Id__c = 'Item id123' ,Product_Code__c ='p1',UDAC__c ='Udac1',Region__c = 'region1',Transaction_Type__c ='New', ownerid = u.id, group_id__c =2 , Product_Family__c='Audio');
           insert FI4;
           Fullfilment_Item__c F = [select ownerID from  Fullfilment_Item__c  where id = :FI4.id ];
           F.ownerID = u.id;   
           update f;
           Query_Request__c qr = new  Query_Request__c(Fulfillment_Item__c = FI4.ID,Request_Comments__c='Test Comment',Request_Reason__c= 'Reason 1');
           insert qr;
        
           Queries__c  qry = new  Queries__c(  Query_Request_ID__c = qr.id ,Query_Reason__c='0845 – PAS – PAS Appointment not setUndo',Reason_Code_Txt__c='URL');
           insert qry;
           Queries__c  qry1 = [Select  Product_Family__c , Reason_Code_Txt__c from Queries__c where id = :qry.id];
           ApexPages.StandardController sc2 = new ApexPages.StandardController(qry1);   
           iFulfillQueryReasonCodeCntrl  controller = new iFulfillQueryReasonCodeCntrl (sc2);
           controller.listQueryReasonCode();
           controller.addQueryReasons();
           
           
       }

}